package com.cbt.cbtBlog.controller;

import com.cbt.cbtBlog.bean.BlogInfo;
import com.cbt.cbtBlog.util.ConstantInfo;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URLEncoder;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping({"/file"})
public class FileDownLoadController {
   @GetMapping({"/downLoadFile"})
   public void downLoadFile(BlogInfo blogInfo, HttpServletResponse response) throws IOException {
      response.reset();
      response.setContentType("application/octet-stream;charset=utf-8");
      String headerValue = "attachment; filename=";
      headerValue = headerValue + URLEncoder.encode(blogInfo.getFileNames().replaceAll(" ", "").split("_")[2], "UTF-8");
      response.setHeader("Content-disposition", headerValue);
      String files = ConstantInfo.BASEPATH;
      files = files + blogInfo.getFileNames();
      BufferedInputStream bis = null;
      BufferedOutputStream bos = null;

      try {
         bis = new BufferedInputStream(new FileInputStream(files));
         bos = new BufferedOutputStream(response.getOutputStream());
         byte[] buff = new byte[1024];
         boolean var8 = false;

         int len;
         while((len = bis.read(buff)) > 0) {
            bos.write(buff, 0, len);
         }
      } catch (FileNotFoundException var13) {
         var13.printStackTrace();
      } catch (IOException var14) {
         var14.printStackTrace();
      } finally {
         bis.close();
         bos.close();
      }

   }
}
